#include"Task.hpp"
#include"RingQueue.hpp"
#include<unistd.h>
#include<time.h>
#include<stdlib.h>
#include<iostream>

void *Consumer(void*args)
{
    RingQueue<Task>* rq=static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        Task task;
        rq->Pop(&task);
        task();
        std::cout<<"comsumer do a task"<<std::endl;
        std::cout<<task.result()<<std::endl;

    }
}
void*Productor(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*> (args);
    while(true)
    {
        sleep(1);
        int x=(rand()+getpid())%100;
        int y=(rand()+(666^getpid()))%100;
        Task task(x,y);
        rq->Push(task);
        std::cout<<"productor make a task"<<std::endl;
    }
}

const int NUM=5;
int main()
{
    srand((unsigned int)time(nullptr)^getpid());
    RingQueue<Task>* rq=new RingQueue<Task>(NUM);
    pthread_t c1,c2,c3,p1,p2,p3;
    pthread_create(&c1,nullptr,Consumer,rq);
    pthread_create(&p1,nullptr,Productor,rq);

    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(c3,nullptr);
    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(p3,nullptr);

    return 0;
}


